package com.gw.booking.controller.backend;

import com.gw.booking.common.MessageCode;
import com.gw.booking.common.ResultObject;
import com.gw.booking.common.Util;
import com.gw.booking.dto.backend.MerchantTypeDto;
import com.gw.booking.service.MerchantTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

@Api(tags = {"后台-商家分类管理"})
@Controller
@RequestMapping("/backend/merchantType")
public class BMerchantTypeController {
    @Autowired
    private MerchantTypeService merchantTypeService;

    @ApiOperation(value = "添加商家分类")
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject add(HttpServletRequest request
            , @Valid @RequestBody MerchantTypeDto dto
            , BindingResult errors)
            throws Exception {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        merchantTypeService.txAdd(Util.getLoginToken(request), dto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "删除商家分类")
    @RequestMapping(value = "/{typeId}/delete.do", method = {RequestMethod.PUT})
    @ResponseBody
    public ResultObject delete(HttpServletRequest request
            , @ApiParam(value = "商家分类id", required = true) @PathVariable Integer typeId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        merchantTypeService.txDelete(Util.getLoginToken(request), typeId);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }
}
